<?php

namespace app\common\job;

use app\common\model\ConsultingComment;
use app\common\model\MemberInfo;
use think\facade\Log;
use think\queue\Job;

/**
 * 评价统计
 * Class ConsultingCommentStatistics
 * @package app\common\job
 */
class ConsultingCommentStatistics
{
    /**
     * fire是消息队列默认调用的方法
     * @param Job $job 当前的任务对象
     * @param array|mixed $data 发布任务时自定义的数据
     */
    public function fire(Job $job, $data)
    {
        $id = $data['id'] ?? null;

        $memberInfo = MemberInfo::where('memberId', $id)->find();
        if (!$memberInfo) {
            $job->delete();
            return;
        }

        try {

            $memberInfo->commentMemberNumber = ConsultingComment::where('lawyerId', $id)->group('memberId')->count();
            $memberInfo->commentLevel = ConsultingComment::where('lawyerId', $id)->avg('level');
            $memberInfo->save();

        } catch (\Exception $exception) {
            Log::error('消费队列：' . $exception->getMessage());
        }

        $job->delete();
    }

}
